import React, { Component } from 'react'
import './home.less'
import { getHomeData } from '../api/home'

export default class BodyPage extends Component {
  constructor(props) {
    super(props)
    this.toDetail = this.toDetail.bind(this)
    this.state = {
      productList: []
    }
  }
  componentDidMount() {
    console.log(this)
    getHomeData({ pageIndex: 1, pageSize: 10 }).then(res => {
      if (res.code === 200) {
        this.setState({
          productList: res.data
        })
      }
    })
  }
  toDetail() {
    this.props.history.push({
      pathname: '/test',
      state: {
        id: 3
      }
    })
  }
  render() {
    return (
      <div className="productList">
        {/* <button onClick={() => this.props.history.push('detail')}>通过函数跳转</button> */}
        {this.state.productList.map(item => (
          <div className="item" key={item.id} onClick={this.toDetail}>
            <img src={item.imageUrl} alt="" />
            <p>{item.wareName}</p>
            <div>
              <span>{item.price}</span>
              <span>{item.originalPrice}</span>
            </div>
          </div>
        ))}
      </div>
    )
  }
}
